<template>
    <div class="edit-popup">
        <popup
            ref="popupRef"
            :title="popupTitle"
            :async="true"
            width="550px"
            @confirm="handleSubmit"
            @close="handleClose"
        >
            <el-form ref="formRef" :model="formData" label-width="80px" :rules="formRules">
<!--                <el-form-item label="分类图片" prop="type">-->
<!--                    <material-picker :limit="1" v-model="formData.cover" />-->
<!--                </el-form-item>-->
                <el-form-item label="分类名称" prop="name">
                    <el-input v-model="formData.name" placeholder="分类名称" />
                </el-form-item>
                <el-form-item label="父级分类" prop="hasPid">
                    <el-radio-group v-model="hasPid">
                        <el-radio :label="0">无父级分类</el-radio>
                        <el-radio :label="1">有父级分类</el-radio>
                    </el-radio-group>
                </el-form-item>
                <el-form-item v-if="hasPid == 1">
                    <el-tree-select
                        :props="{ label: 'name', value: 'id' }"
                        class="flex-1"
                        v-model="formData.pid"
                        :data="optionsData.category.list"
                        clearable
                        :default-expand-all="true"
                        placeholder="请选择父级分类"
                        check-strictly
                    />
                </el-form-item>
                <el-form-item label="排序" prop="icon">
                    <el-input v-model="formData.sort" />
                    <div class="form-tips">默认为0，数值越大越排前面</div>
                </el-form-item>
                <el-form-item label="状态" prop="status">
                    <el-switch v-model="formData.status" :active-value="1" :inactive-value="0" />
                </el-form-item>
            </el-form>
        </popup>
    </div>
</template>
<script lang="ts" setup>
import type { FormInstance } from 'element-plus'
import {  shopCatListAdd, shopCatListDetail, shopCatListEdit,shopCatListTree } from '@/api/goods'
import Popup from '@/components/popup/index.vue'
import { useDictOptions } from '@/hooks/useDictOptions'
const emit = defineEmits(['success', 'close'])
const formRef = shallowRef<FormInstance>()
const popupRef = shallowRef<InstanceType<typeof Popup>>()
const mode = ref('add')
const popupTitle = computed(() => {
    return mode.value == 'edit' ? '编辑分类' : '新增分类'
})
const hasPid = ref(0)
const formData = reactive({
    id: '',
    name: '',
    pid: '',
    cover: '',
    sort: 0,
    status: 1,
	remark:''
})

const formRules = {
    name: [
        {
            required: true,
            message: '请输入分类名称',
            trigger: 'blur'
        }
    ],
    status: [
        {
            required: true,
            message: '请选择状态',
            trigger: 'blur'
        }
    ]
}

const { optionsData } = useDictOptions<{
    category: any[]
}>({
    category: {
        api: shopCatListTree
    }
})
const open = (type = 'add') => {
    mode.value = type
    popupRef.value?.open()
}

const setFormData = (data: Record<any, any>) => {
    for (const key in formData) {
        if (data[key] != null && data[key] != undefined) {
            //@ts-ignore
            formData[key] = data[key]
        }
    }
    if (data.pid) {
        hasPid.value = 1
    } else {
        //@ts-ignore
        formData['pid'] = ''
    }
}

const getDetail = async (row: Record<string, any>) => {
    const data = await shopCatListDetail({
        id: row.id
    })
    setFormData(data)
}

const handleClose = () => {
    emit('close')
}

defineExpose({
    open,
    setFormData,
    getDetail
})
//提交
const handleSubmit = async () => {
    if (hasPid.value == 0) {
        //@ts-ignore
        formData.pid = 0
    }
    await formRef.value?.validate()
    mode.value == 'edit' ? await shopCatListEdit(formData) : await shopCatListAdd(formData)
    popupRef.value?.close()
    emit('success')
}
</script>
